﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoint : MonoBehaviour
{
    [SerializeField] private Vector3[] points; //定义了一个序列化的私有数组 points，用于存储路径点的位置信息（[SerializeField]目的是使私有变量在编辑器中可见）

    public Vector3[] Points
    {
        get
        {
            return points;
        }
    }
    public Vector3 CurrentPosition  //公有属性，确保在别的类中能获得该类的私有变量_currentPosition
    {
        get
        {
            return _currentPosition;
        }
    }
    private Vector3 _currentPosition;  //Waypoint对象的初始位置(世界坐标系下)
    private bool _gameStarted;

    // Start is called before the first frame update
    void Start()
    {
        _gameStarted = true;
        _currentPosition = transform.position;
    }

    // Update is called once per frame
    public Vector3 GetWaypointPosition(int index)
    {
        return CurrentPosition + Points[index];   //（Points是相对于Waypoint对象的坐标）这段代码返回值就是标记点的世界坐标
    }

    private void OnDrawGizmos()  //绘画
    {  
        if (_gameStarted == false && transform.hasChanged)  //游戏未开始或者Waypoint的对象移动时
        {
            _currentPosition = transform.position;    //对象的当前位置改变（同步）
        }
        
        for(int i = 0; i < points.Length; i++)   //循环，把所有的坐标点绘制出来，在每个坐标点的世界坐标位置绘制一个大小为0 .5f的圆
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(points[i] + _currentPosition, 0.5f);

            if (i < points.Length - 1)   //给每个坐标点绘制连线
            {
                Gizmos.color = Color.gray;
                Gizmos.DrawLine(points[i] + _currentPosition, points[i + 1] + _currentPosition);  //由于_currentPosition是与transform.position同步的，所以整个绘画会随Waypoint对象位置的改变而改变
            }
        }    
    }
}
